package com.cgsd.heyu.utils;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.lang.ref.WeakReference;

/**
 * @author xiaoxing
 */
public class WeakHandler extends Handler {

    public interface IHandler {
        /**
         * 消息
         */
        void handleMsg(Message msg);
    }

    /**
     *弱引用
     */
    private final WeakReference<IHandler> mRef;


    public WeakHandler(IHandler handler) {
        super(Looper.getMainLooper());
        mRef = new WeakReference<>(handler);
    }

    @SuppressWarnings("unused")
    @Override
    public void handleMessage(Message msg) {
        IHandler handler = mRef.get();
        if (handler != null && msg != null) {
            handler.handleMsg(msg);
        }
    }
}